package com.dagene.weixin.web;

import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.dagene.weixin.JSONMessage;
import com.dagene.weixin.entity.Newstable;
import com.dagene.weixin.service.NewstableService;
import com.dagene.weixin.util.Constants;

@Controller
@RequestMapping(value = "/article")
public class NewsController {
	@Autowired
	NewstableService newstableService;

	/**
	 * 上传图片
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/uploadImg", method = RequestMethod.POST)
	@ResponseBody
	public JSONMessage uploadImg(HttpServletRequest request, @RequestParam int id) {
		JSONMessage message = new JSONMessage();
		try {
			if (request.getSession().getAttribute("admin") == null) {
				message.setCode(Constants.SESSION_TIMEOUT);
			} else {
				Newstable article = null;
				if (id == 0) {
					article = new Newstable();
				} else {
					article = newstableService.getNewsById(id);
				}

				MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
				List<MultipartFile> fileList = multipartRequest
						.getFiles("file");
				if (fileList.size() > 0) {
					MultipartFile mf = fileList.get(0);
					String fileName = mf.getOriginalFilename();
					fileName = fileName.substring(fileName.lastIndexOf("."));
					System.out.println(fileName);
					fileName = UUID.randomUUID().toString().replaceAll("-", "")
							+ fileName;
					System.out.println(fileName);

					// 保存在服务器上的路径
					String filepath = File.separator + "upload"
							+ File.separator + "article";
					String url = request.getSession().getServletContext()
							.getRealPath(filepath);
					File folder = new File(url);
					folder.mkdirs();
					File file = new File(url, fileName);
					mf.transferTo(file);

					

					article.setHeadImg(".." + filepath + File.separator
							+ fileName);
					article = newstableService.save(article);
					message.setData(article);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			message.setCode(Constants.ERROR_CODE);
			message.setData(e.getMessage());
		}
		return message;
	}

	@RequestMapping(value = "/save", method = RequestMethod.POST)
	@ResponseBody
	public JSONMessage save(HttpServletRequest request,
			@RequestBody Newstable newstable) {
		JSONMessage message = new JSONMessage();
		try {
			if (request.getSession().getAttribute("admin") == null) {
				message.setCode(Constants.SESSION_TIMEOUT);
			} else {
				newstable.setCreateTime(new Date());
				newstable = newstableService.save(newstable);
				message.setData(newstable);
			}
		} catch (Exception e) {
			e.printStackTrace();
			message.setCode(Constants.ERROR_CODE);
			message.setData(e.getMessage());
		}
		return message;
	}

	/**
	 * 分页查询文章
	 * 
	 * @param request
	 * @param type
	 *            文章类型
	 * @param pageSize
	 * @param pageIndex
	 * @return
	 */
	@RequestMapping(value = "/getNewsByPage", method = RequestMethod.GET)
	@ResponseBody
	public JSONMessage getNewsByPage(HttpServletRequest request,
			short type, int pageSize, int pageIndex) {
		JSONMessage message = new JSONMessage();
		try {
			Pageable page = new PageRequest(pageIndex, pageSize);
			Page<Newstable> list = newstableService.getNewsByPage(type, page);
			message.setData(list.getContent());
			message.setTotal((int) list.getTotalElements());
		} catch (Exception e) {
			e.printStackTrace();
			message.setCode(Constants.ERROR_CODE);
			message.setMessage(e.getMessage());
		}
		return message;
	}

	@RequestMapping(value = "/getNewsByType", method = RequestMethod.GET)
	@ResponseBody
	public JSONMessage getNewsByType(HttpServletRequest request, short type) {
		JSONMessage message = new JSONMessage();
		try {
			message.setData(newstableService.getNewsByType(type));
		} catch (Exception e) {
			e.printStackTrace();
			message.setCode(Constants.ERROR_CODE);
			message.setMessage(e.getMessage());
		}
		return message;
	}

	@RequestMapping(value = "/getNewsDetail", method = RequestMethod.GET)
	@ResponseBody
	public JSONMessage getNewsDetail(HttpServletRequest request, int id) {
		JSONMessage message = new JSONMessage();
		try {
			message.setData(newstableService.getNewsById(id));
		} catch (Exception e) {
			e.printStackTrace();
			message.setCode(Constants.ERROR_CODE);
			message.setMessage(e.getMessage());
		}
		return message;
	}

	@RequestMapping(value = "/queryNews", method = RequestMethod.GET)
	@ResponseBody
	public JSONMessage queryNews(HttpServletRequest request,
			@RequestParam String title, @RequestParam String type) {
		JSONMessage message = new JSONMessage();
		try {
			message.setData(newstableService.queryNews(title, type));
		} catch (Exception e) {
			e.printStackTrace();
			message.setCode(Constants.ERROR_CODE);
			message.setMessage(e.getMessage());
		}
		return message;
	}

	@RequestMapping(value = "/deleteNews", method = RequestMethod.GET)
	@ResponseBody
	public JSONMessage deleteNews(HttpServletRequest request,
			@RequestParam int id) {
		JSONMessage message = new JSONMessage();
		try {
			if (request.getSession().getAttribute("admin") == null) {
				message.setCode(Constants.SESSION_TIMEOUT);
			} else {
				newstableService.deleteNews(id);
			}
		} catch (Exception e) {
			e.printStackTrace();
			message.setCode(Constants.ERROR_CODE);
			message.setMessage(e.getMessage());
		}
		return message;
	}

	
}
